package controller

import (
	"fmt"
	"gitee.com/Frank098/devops-app-mgt/utils"
	"gitee.com/Frank098/devops-app-mgt/utils/log"
	"github.com/gin-gonic/gin"
	"net/http"
)

func Login(ctx *gin.Context) {
	params := new(struct {
		UserName string `form:"username"`
	})
	if err := ctx.ShouldBindJSON(params); err != nil {
		log.Error("Bind请求参数失败, " + err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	//生成token并返回
	token, err := utils.GenToken(params.UserName)
	if err != nil {
		fmt.Println("Login() GenToken err:", err.Error())
		return
	}
	m := map[string]string{
		"token": token,
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "token获取成功",
		"data": m,
	})
	return
}
